package com.learn.aop.locator.repository.impl;

import com.learn.aop.model.ProxyMetaDefinition;
import org.springframework.util.StringUtils;


import java.util.*;

import static java.util.Collections.synchronizedMap;

/**
 * @description: java类作用描述
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2024/1/7 22:14
 * @updateUser: cong zhi
 * @updateTime: 2024/1/7 22:14
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class InMemoryProxyMetaDefinitionRepository extends BaseProxyMetaDefinitionRepository {

    private final Map<String, ProxyMetaDefinition> proxyMetaDefinitionMap = synchronizedMap(new LinkedHashMap());

    @Override
    public List<ProxyMetaDefinition> getProxyMetaDefinitions() {
        return new ArrayList<>(proxyMetaDefinitionMap.values());
    }

    @Override
    public ProxyMetaDefinition getProxyMetaDefinition(String proxyMetaDefinitionId) {
        return proxyMetaDefinitionMap.get(proxyMetaDefinitionId);
    }

    @Override
    public boolean saveProxyMetaDefinition(ProxyMetaDefinition definition) {
        if (StringUtils.isEmpty(definition.getId())) {
            throw new IllegalArgumentException("Id can not be empty");
        }
        proxyMetaDefinitionMap.put(definition.getId(), definition);
        return true;
    }

    @Override
    public boolean deleteProxyMetaDefinition(String proxyMetaDefinitionId) {
        if (proxyMetaDefinitionMap.containsKey(proxyMetaDefinitionId)) {
            proxyMetaDefinitionMap.remove(proxyMetaDefinitionId);
        } else {
            throw new NoSuchElementException("ProxyMetaDefinition not found: " + proxyMetaDefinitionId);
        }
        return true;
    }
}
